﻿<ad:DockableContent x:Class="ReflectionStudio.Components.UserControls.DatabaseExplorer"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:ad="clr-namespace:AvalonDock;assembly=AvalonDock"
        mc:Ignorable="d"
        xmlns:controls="clr-namespace:ReflectionStudio.Controls;assembly=ReflectionStudio.Controls"
        xmlns:local="clr-namespace:ReflectionStudio.Components.UserControls"
        xmlns:converters="clr-namespace:ReflectionStudio.Components.Converters"
        Title="Databases" IsCloseable="true"
        d:DesignHeight="300" d:DesignWidth="300"
        Icon="/ReflectionStudio;component/Resources/Images/16x16/database/db.png">
    <ad:DockableContent.Resources>

        <Style x:Key="{x:Type controls:TreeViewItemExtended}" TargetType="{x:Type controls:TreeViewItemExtended}">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Foreground" Value="{DynamicResource NormalForegroundBrush}"/>
            <Setter Property="FocusVisualStyle" Value="{DynamicResource TreeViewExtendedItemFocusVisual}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type controls:TreeViewItemExtended}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition MinWidth="19" Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <ToggleButton x:Name="Expander" Style="{DynamicResource TreeViewExtendedItemExpandCollapseToggleStyle}"
                                IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"
                                ClickMode="Press"/>
                            <Border Name="Bd"
                                  Grid.Column="1"
                                  Background="{TemplateBinding Background}"
                                  BorderBrush="{TemplateBinding BorderBrush}"
                                  BorderThickness="{TemplateBinding BorderThickness}"
                                  Padding="{TemplateBinding Padding}">
                                <StackPanel Orientation="Horizontal">
                                    <Image VerticalAlignment="Center" Width="16" Height="16" Margin="0,0,5,0"
                                           Source="{TemplateBinding Tag,  Converter={x:Static converters:DBTypeToImageConverter.Instance}}" />
                                    <ContentPresenter x:Name="PART_Header"
                                          ContentSource="Header"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                                </StackPanel>
                            </Border>
                            <ItemsPresenter x:Name="ItemsHost"
                                  Grid.Row="1"
                                  Grid.Column="1"
                                  Grid.ColumnSpan="2"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsExpanded" Value="false">
                                <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="HasItems" Value="false">
                                <Setter TargetName="Expander" Property="Visibility" Value="Hidden"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="HasHeader" Value="false"/>
                                    <Condition Property="Width" Value="Auto"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="PART_Header" Property="MinWidth" Value="75"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="HasHeader" Value="false"/>
                                    <Condition Property="Height" Value="Auto"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="PART_Header" Property="MinHeight" Value="19"/>
                            </MultiTrigger>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="Bd" Property="Background" Value="{DynamicResource SelectedBackgroundBrush}"/>
                                <Setter Property="Foreground" Value="{DynamicResource SelectedForegroundBrush}"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="true"/>
                                    <Condition Property="IsSelectionActive" Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Bd" Property="Background" Value="{DynamicResource SelectedBackgroundBrush}"/>
                                <Setter Property="Foreground" Value="{DynamicResource SelectedForegroundBrush}"/>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource DisabledBackgroundBrush}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </ad:DockableContent.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="28" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        
        <DockPanel>
            <DockPanel>
                <controls:DropDownButton Content="Connect..." Name="DropDownMenu" ToolTip="Connect to a registered data source in the workspace">
                    <ButtonBase.ContextMenu>
                        <ContextMenu ItemsSource="{Binding Workspace.DataSources}">
                            <ContextMenu.ItemTemplate>
                                    <DataTemplate>
                                        <MenuItem Header="{Binding Name}" Click="MenuItem_Click"/>
                                    </DataTemplate>
                            </ContextMenu.ItemTemplate>
                        </ContextMenu>    
                    </ButtonBase.ContextMenu>
                </controls:DropDownButton>
            </DockPanel>
            
            <StackPanel Orientation="Horizontal">
    		    <controls:FlatImageButton
    			    Command="{x:Static local:DatabaseExplorer.DataSourceAdd}"
    			    Image="/ReflectionStudio;component/Resources/Images/16x16/database/new_db.png"
                    ToolTip="Add a new data source to the workspace"/>

    		    <controls:FlatImageButton
    			    Command="{x:Static local:DatabaseExplorer.DataSourceRefresh}"
    			    Image="/ReflectionStudio;component/Resources/Images/16x16/application/refresh.png"
                    ToolTip="Refresh the current data source"/>

    		    <controls:FlatImageButton
    			    Command="{x:Static local:DatabaseExplorer.DataSourceRemove}"
    			    Image="/ReflectionStudio;component/Resources/Images/16x16/application/delete.png" 
                    ToolTip="Delete the current data source from the workspace"/>
			</StackPanel>
            
    	</DockPanel>

        <controls:TreeViewExtended x:Name="treeViewDB" Grid.Row="2" Tag="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem.Tag}"
                                   SelectedItemChanged="treeViewDB_SelectedItemChanged" PopulateOnDemand="True">
            <TreeView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Refresh"
                              DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                              Command="{x:Static local:DatabaseExplorer.DataSourceRefresh}"
                              CommandParameter="{Binding Tag}"
                              IsEnabled="{Binding Tag,  Converter={x:Static converters:ObjectToBooleanConverter.Instance}}" />
                    <MenuItem Header="Delete"
                              DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                              Command="{x:Static local:DatabaseExplorer.DataSourceRefresh}"
                              CommandParameter="{Binding Tag}"
                              IsEnabled="{Binding Tag,  Converter={x:Static converters:ObjectToBooleanConverter.Instance}}">
                        <MenuItem.Icon>
                            <controls:AutoGreyableImage Source="/ReflectionStudio;component/Resources/Images/16x16/application/delete.png" Width="16"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <Separator/>
                    <MenuItem Header="New Query"
                              Command="{x:Static local:DatabaseExplorer.QueryNewEditor}"/>
                    <MenuItem Header="Script...">
                        <MenuItem Header="CREATE" Command="{x:Static local:DatabaseExplorer.QueryScriptCreate}"/>
                        <MenuItem Header="ALTER" Command="{x:Static local:DatabaseExplorer.QueryScriptAlter}"/>
                        <MenuItem Header="DROP" Command="{x:Static local:DatabaseExplorer.QueryScriptDrop}"/>
                        <Separator/>
                        <MenuItem Header="SELECT" Command="{x:Static local:DatabaseExplorer.QueryScriptSelect}"/>
                        <MenuItem Header="INSERT" Command="{x:Static local:DatabaseExplorer.QueryScriptInsert}"/>
                        <MenuItem Header="UPDATE" Command="{x:Static local:DatabaseExplorer.QueryScriptUpdate}"/>
                        <MenuItem Header="DELETE" Command="{x:Static local:DatabaseExplorer.QueryScriptDelete}"/>
                    </MenuItem>
                    <Separator/>
                    <MenuItem Header="View dependencies"
                              DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                              Command="{x:Static local:DatabaseExplorer.QueryViewDependencies}"
                              CommandParameter="{Binding Tag}"
                              IsEnabled="{Binding Tag,  Converter={x:Static converters:ObjectToBooleanConverter.Instance}}"/>
                    <Separator/>
                    <MenuItem Header="Control quality"
                              DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                              Command="{x:Static local:DatabaseExplorer.QueryControlQuality}"
                              CommandParameter="{Binding Tag}"
                              />
                    <Separator/>
                    <MenuItem Header="Properties"
                              DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                              Command="ApplicationCommands.Properties"
                              CommandParameter="{Binding Tag}"
                              IsEnabled="{Binding Tag,  Converter={x:Static converters:ObjectToBooleanConverter.Instance}}">
                        <MenuItem.Icon>
                            <controls:AutoGreyableImage Source="/ReflectionStudio;component/Resources/Images/16x16/properties.png" Width="16"/>
                        </MenuItem.Icon>
                    </MenuItem>
                </ContextMenu>
            </TreeView.ContextMenu>
        </controls:TreeViewExtended>
    </Grid>
</ad:DockableContent>
